package com.example.administrator.recyclerviewtest_407xjy.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.recyclerviewtest_407xjy.R;
import com.example.administrator.recyclerviewtest_407xjy.bean.TcaseBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2017/4/13.
 */

public class TcaseAdapter extends RecyclerView.Adapter {
    private Context context;//上下文
    private LayoutInflater layoutInflater;//动态加载布局
    private  List<TcaseBean> list;

    //自定义 构造方法 传送上下文
    public TcaseAdapter(Context context) {
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }
    //获取数据
    public void setList(List<TcaseBean>list)
    {
        this.list=list;

    }
    //3:viewholder子类
    public class ViewHolder extends RecyclerView.ViewHolder
    {
        private ImageView imageView;
        private TextView tvtitle,tvdescrrpt,tvtime;
        private Button button;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView=(ImageView)itemView.findViewById(R.id.imageView);
            tvtitle=(TextView)itemView.findViewById(R.id.textView);
            tvdescrrpt=(TextView)itemView.findViewById(R.id.textView2);
            tvtime=(TextView)itemView.findViewById(R.id.textView3);
            button=(Button) itemView.findViewById(R.id.button);

        }
    }

    //重写:生成view
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=layoutInflater.inflate(R.layout.item,parent,false);

        return new ViewHolder(v);
    }

    //重写 给view 中的控件填充值 设置监听
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        TcaseBean tcaseBean=list.get(position);
        if(tcaseBean==null)
            return;
        ViewHolder viewHolder=(ViewHolder)holder;
        //viewHolder.imageView.setImageResource(tcaseBean.getImgid());
        viewHolder.tvtitle.setText(tcaseBean.getName());
        viewHolder.tvdescrrpt.setText(tcaseBean.getDescription());
        viewHolder.tvtime.setText(tcaseBean.getUpdate_time());
//异步加载图片
        Picasso.with(context).load("http://amicool.neusoft.edu.cn/Uploads/"
                +tcaseBean.getThumb()).placeholder(R.mipmap.ic_launcher)
                .into(viewHolder.imageView);



        viewHolder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.remove(position);
                notifyDataSetChanged();//更新recycview
                //实际案例中，要删除涉及到数据库网络
                //要有删除前的提醒

            }
        });
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取当前条目信息,如id
                int id=list.get(position).getId();
                Toast.makeText(context, ""+id, Toast.LENGTH_SHORT).show();
                //实际中，打开后新窗口做其他操作



            }
        });

    }
    //共有多少条目数
    @Override
    public int getItemCount() {
        if (list==null) return 0;

       else return list.size();
    }
}
